package io.github.karino2.pngnote.book;

import android.content.ContentResolver;
import android.content.Context;
import android.database.Cursor;
import android.net.Uri;
import android.provider.DocumentsContract;

import java.io.FileNotFoundException;
import java.util.ArrayList;
import java.util.Date;
import java.util.Iterator;
import java.util.List;

public class FastFile {
    private Uri uri;
    private String name;
    private long lastModified;
    private String mimeType;
    private long size;
    private ContentResolver resolver;

    public FastFile(Uri uri, String name, long lastModified, String mimeType,
                    long size, ContentResolver resolver) {
        this.uri = uri;
        this.name = name;
        this.lastModified = lastModified;
        this.mimeType = mimeType;
        this.size = size;
        this.resolver = resolver;
    }

    public Uri getUri() {
        return this.uri;
    }
    public String getName() {
        return this.name;
    }
    public long getLastModified() {
        return this.lastModified;
    }
    public String getMimeType() {
        return this.mimeType;
    }
    public long getSize() {
        return this.size;
    }
    public ContentResolver getResolver() {
        return this.resolver;
    }

    public boolean isDirectory() {
        return "vnd.android.document/directory".equals(this.mimeType);
    }

    public boolean isFile() {
        return !this.isDirectory() && !"".equals(this.mimeType);
    }

    public FastFile createFile( String fileMimeType, String fileDisplayName) {
        Uri var3 = null;
        try {
            var3 = DocumentsContract.createDocument(this.resolver, this.uri, fileMimeType, fileDisplayName);
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        }
        FastFile var10000;
        if (var3 != null) {
            Uri it = var3;
            var10000 = new FastFile(it, fileDisplayName, (new Date()).getTime(), fileMimeType, 0L, this.resolver);
        } else {
            var10000 = null;
        }

        return var10000;
    }

    public List<FastFile> listFiles() {
        return listFiles(this.resolver, this.uri);
    }

    public FastFile findFile(String targetDisplayName) {
        List<FastFile> var2 = this.listFiles();
        Iterator var4 = var2.iterator();

        Object var10000;
        while(true) {
            if (var4.hasNext()) {
                Object var5 = var4.next();
                FastFile it = (FastFile)var5;
                if (it.name != null && targetDisplayName != null &&
                        !it.name.equals(targetDisplayName)) { //FIXME:???
                    continue;
                }
                var10000 = var5;
                break;
            }

            var10000 = null;
            break;
        }

        return (FastFile)var10000;
    }

    public FastFile createDirectory(String displayName) {
        Uri uri = null;
        try {
            uri = DocumentsContract.createDocument(this.resolver, this.uri,
                    "vnd.android.document/directory", displayName);
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        }
        return uri != null ? fromDocUri(this.resolver, uri) : null;
    }

    public boolean isEmpty() {
        if (!this.isFile()) {
            return false;
        } else {
            return 0L == this.size;
        }
    }

    public Uri component1() {
        return this.uri;
    }
    public String component2() {
        return this.name;
    }
    public long component3() {
        return this.lastModified;
    }
    public String component4() {
        return this.mimeType;
    }
    public long component5() {
        return this.size;
    }
    public ContentResolver component6() {
        return this.resolver;
    }

    public FastFile copy(Uri uri, String name, long lastModified, String mimeType,
                               long size, ContentResolver resolver) {
        return new FastFile(uri, name, lastModified, mimeType, size, resolver);
    }

    //FIXME:removed this
    // $FF: synthetic method
    public static FastFile copy$default(FastFile var0, Uri var1, String var2, long var3, String var5, long var6, ContentResolver var8, int var9, Object var10) {
        if ((var9 & 1) != 0) {
            var1 = var0.uri;
        }

        if ((var9 & 2) != 0) {
            var2 = var0.name;
        }

        if ((var9 & 4) != 0) {
            var3 = var0.lastModified;
        }

        if ((var9 & 8) != 0) {
            var5 = var0.mimeType;
        }

        if ((var9 & 16) != 0) {
            var6 = var0.size;
        }

        if ((var9 & 32) != 0) {
            var8 = var0.resolver;
        }

        return var0.copy(var1, var2, var3, var5, var6, var8);
    }

    //FIXME:
    @Override
    public String toString() {
        return "FastFile(uri=" + this.uri + ", name=" + this.name + ", lastModified=" + this.lastModified + ", mimeType=" + this.mimeType + ", size=" + this.size + ", resolver=" + this.resolver + ')';
    }

    //FIXME:
    @Override
    public int hashCode() {
        int result = this.uri.hashCode();
        result = result * 31 + this.name.hashCode();
        result = result * 31 + Long.hashCode(this.lastModified);
        result = result * 31 + this.mimeType.hashCode();
        result = result * 31 + Long.hashCode(this.size);
        result = result * 31 + this.resolver.hashCode();
        return result;
    }

    //FIXME:
    @Override
    public boolean equals(Object other) {
        if (this == other) {
            return true;
        } else if (!(other instanceof FastFile)) {
            return false;
        } else {
            FastFile o2 = (FastFile)other;
            if (this.uri != null && o2.uri != null &&
                    !this.uri.equals(o2.uri)) {
                return false;
            } else if (this.name != null && o2.name != null &&
                    !this.name.equals(o2.name)) {
                return false;
            } else if (this.lastModified != o2.lastModified) {
                return false;
            } else if (this.mimeType != null && o2.mimeType != null &&
                    !this.mimeType.equals(o2.mimeType)) {
                return false;
            } else if (this.size != o2.size) {
                return false;
            } else {
                return this.resolver != null && o2.resolver != null &&
                        this.resolver.equals(o2.resolver);
            }
        }
    }

    private static long getLong(Cursor cur, String columnName) {
        int index = cur.getColumnIndex(columnName);
        return cur.isNull(index) ? 0L : cur.getLong(index);
    }

    private static String getString(Cursor cur, String columnName) {
        int index = cur.getColumnIndex(columnName);
        if (cur.isNull(index)) {
            return "";
        } else {
            String var4 = cur.getString(index); //check null
            return var4;
        }
    }

    private static FastFile fromCursor(Cursor cur, Uri uri, ContentResolver resolver) {
        String disp = getString(cur, "_display_name");
        long lm = getLong(cur, "last_modified");
        String mimeType = getString(cur, "mime_type");
        long size = getLong(cur, "_size");
        FastFile file = new FastFile(uri, disp, lm, mimeType, size, resolver);
        return file;
    }

    public List<FastFile> listFiles(ContentResolver resolver, Uri parent) {
        Uri childrenUri = DocumentsContract.buildChildDocumentsUriUsingTree(parent, DocumentsContract.getDocumentId(parent));
        Cursor cur = resolver.query(childrenUri, null,
                null, null, null,
                null);
        List<FastFile> result = new ArrayList<>();
        while(cur.moveToNext()) {
            String docId = cur.getString(0);
            Uri uri = DocumentsContract.buildDocumentUriUsingTree(parent, docId);
            result.add(fromCursor(cur, uri, resolver));
        }
        return result;
    }

    public static FastFile fromTreeUri(Context context, Uri treeUri) {
        String var4 = DocumentsContract.isDocumentUri(context, treeUri) ? DocumentsContract.getDocumentId(treeUri) : DocumentsContract.getTreeDocumentId(treeUri);
        if (var4 == null) {
            throw new IllegalArgumentException("Could not get documentUri from " + treeUri);
        } else {
            String docId = var4;
            Uri var5 = DocumentsContract.buildDocumentUriUsingTree(treeUri, docId);
            if (var5 == null) {
                throw new NullPointerException("Failed to build documentUri from " + treeUri);
            } else {
                Uri treeDocUri = var5;
                ContentResolver resolver = context.getContentResolver(); //FIXME：check null
                FastFile var6 = fromDocUri(resolver, treeDocUri);
                if (var6 != null) {
                    return var6;
                } else {
                    throw new IllegalArgumentException("Could not query from " + treeUri);
                }
            }
        }
    }

    public static FastFile fromDocUri(ContentResolver resolver, Uri treeDocUri) {
        Cursor cur = null;
        try {
            cur = resolver.query(treeDocUri, null,
                    null, null, null,
                    null);
            if (cur != null && cur.moveToFirst()) {
                FastFile var8 = fromCursor(cur, treeDocUri, resolver);
                return var8;
            }
        } catch (Throwable e) {
            e.printStackTrace();
        } finally {
            if (cur != null) {
                cur.close();
            }
        }
        return null;
    }
}
